/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package system.ai;

import com.aionemu.gameserver.ai.AIClasss;
import com.aionemu.gameserver.ai.CreatureAI;
import com.aionemu.gameserver.model.gameobjects.Creature;
import com.aionemu.gameserver.model.gameobjects.VisibleObject;
import com.aionemu.gameserver.model.gameobjects.instance.Trap;
import com.aionemu.gameserver.skillengine.SkillEngine;
import com.aionemu.gameserver.skillengine.model.Skill;
import com.aionemu.gameserver.utils.MathUtil;

/**
 * @author Mr. Poke
 *
 */
public class TrapAI extends CreatureAI
{

	private boolean	thinking	= false;
	//Despawn time 1 min
	private int DESPAWN_TIME = 60;
	/**
	 * @param owner
	 */
	public TrapAI(Creature owner)
	{
		super(owner);
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.ai.CreatureAI#getOwner()
	 */
	@Override
	public Trap getOwner()
	{
		return (Trap)super.getOwner();
	}

	@Override
	protected void onIntentionIdle()
	{
	}

	@Override
	protected void onIntentionActive()
	{
	}
	
	@Override
	protected void onEvtThink()
	{
		if (thinking || getOwner().isCasting() || getOwner().getMaster() == null)
			return;
		
		thinking = true;
		try
		{
			DESPAWN_TIME = DESPAWN_TIME-1;
			if (DESPAWN_TIME < 1)
			{
				getOwner().onDespawn();
				return;
			}
			for (VisibleObject visibleObject : getOwner().getKnownList().getKnownObjects().values())
			{
				if (!(visibleObject instanceof Creature))
					continue;
				Creature creature = (Creature)visibleObject;
				if (!getOwner().isEnemy(creature) || creature.getLifeStats().isAlreadyDead())
					continue;
				float range = (float) MathUtil.getDistance(creature, owner)-getOwner().getBoundingRadius().getCollision()-creature.getBoundingRadius().getCollision();
				if (range > 1)
					continue;
				Skill skill = SkillEngine.getInstance().getSkill(getOwner(), getOwner().getSkillId(), 1, creature);
				if (skill != null)
				{
					if (skill.useSkill())
					{
						DESPAWN_TIME = 0;
						break;
					}
				}
			}
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			// Stop thinking action
			thinking = false;
		}
	}
	public static void main(String[] args)
	{
		AIClasss.getInstance().addClass("TrapAI", TrapAI.class);
	}
}
